package leedcode_path.Link;

public class 链表相交 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int lenA=0,lenB=0;
        ListNode dummyA=new ListNode(0,headA);
        ListNode dummyB=new ListNode(0,headB);
        ListNode curA=dummyA;
        ListNode curB=dummyB;
        while (curA.next!=null){
            curA=curA.next;
            lenA++;
        }
        while (curB.next!=null){
            curB=curB.next;
            lenB++;
        }
        curA=dummyA;
        curB=dummyB;
        if (lenA>lenB){
            int len=lenA-lenB;
            while (len!=0){
                curA=curA.next;
                len--;
            }
        }
        if (lenA<lenB) {
            int len=lenB-lenA;
            while (len!=0){
                curB=curB.next;
                len--;
            }
        }
        while (curA.next!=null){
            curA=curA.next;
            curB=curB.next;
            if (curA==curB){
                return curA;
            }
        }
        return null;

    }
}
